package cn.icare.cms.controller.restful;

import cn.icare.cms.CouponService;
import cn.icare.cms.aop.UserLog;
import cn.icare.cms.model.ro.AddCouponRO;
import cn.icare.cms.model.ro.EditCouponRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/coupon")
@Api(tags = "REST优惠券")
public class CouponController extends BaseController {
    @Resource
    private CouponService couponService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/coupon
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个优惠券", notes = "t_Coupon表新增单个优惠券")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddCouponRO ro, HttpServletRequest request) {
        String userName = this.getCurrentUserName(request);
        Integer id = this.couponService.createOne(ro, userName);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/coupon
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个优惠券", notes = "更新单个优惠券")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditCouponRO ro, HttpServletRequest request) {
        String userName = this.getCurrentUserName(request);
        if (this.couponService.updateOne(ro, userName)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/coupon/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个优惠券", notes = "删除符合主键的单个优惠券")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Integer id) {
        if (this.couponService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/coupon?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个优惠券", notes = "删除符合主键的多个优惠券")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Integer> ids) {
        if (this.couponService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/coupon/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个优惠券", notes = "返回符合主键的单个优惠券")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.couponService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/coupon?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个优惠券", notes = "user表返回符合条件的多个优惠券")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "type", required = false) Integer type,
                                             @RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize
    ) {
        return super.getRespOk(this.couponService.getList(name, type, pageNum, pageSize));
    }


    /**
     * URL:http://{ip}:{port}/rest/coupon
     * method:POST
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/updatestatus")
    @UserLog(desc = "更新优惠券状态")
    @ApiOperation(value = "更新优惠券状态", notes = "更新优惠券状态")
    public ResponseEntity<RespMsgVO> updateStatus(@RequestParam(name = "id") Integer id,
                                                  @RequestParam("status") Integer status) {
        if (this.couponService.updateOneStatus(id, status)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }


    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddCouponRO ro, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditCouponRO ro, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Integer> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Integer type, String name, Integer pageNum, Integer pageSize) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
